/**
 * pop div
 */
(function($){
$.fn.extend({

	popSet:{
		closeId:'',
		titleId:'',
		backDivColor:'#333',
		showAnimate:true, 
		backDivId:'pop__div__id'
	},

	popShow:function(setObj){

		var divDragGable = function(titleDivid , dragDivid)
		{
			document.getElementById(titleDivid).style.cursor = "move";
			document.getElementById(titleDivid).onmousedown=function(e){
				var posX;
				var posY;  
				var fdiv = document.getElementById(dragDivid); 
				if(!e) e = window.event;
				posX = e.clientX - parseInt(fdiv.style.left);
				posY = e.clientY - parseInt(fdiv.style.top);
				document.onmousemove = function(ev){
					if(ev==null) ev = window.event;
					fdiv.style.left = (ev.clientX - posX) + "px";
					fdiv.style.top = (ev.clientY - posY) + "px";
					return false;
				}
				document.onmouseup = function(){
					document.onmousemove = null;
				}
			}
		}
		var setDivPox = function(jqBackDiv,jqPopDiv,isScrollEvent)
		{
			var bodyObj = document.documentElement;
			if(document.compatMode == 'BackCompat'){
				bodyObj = document.body;
			}
			jqBackDiv.width($(window).width());
			jqBackDiv.height(Math.max(bodyObj.scrollHeight, document.documentElement.clientHeight));
			
			var dleft = bodyObj.clientWidth/2 - jqPopDiv.width()/2;
			var dtop = bodyObj.clientHeight/2 - jqPopDiv.height()/2;
			dleft += $(window).scrollLeft();
			dtop += $(window).scrollTop();
			jqPopDiv.css({left:dleft,top:dtop,position:'absolute',zIndex:1001});
		}

		if(typeof(setObj.closeId) != 'undefined'){
			this.popSet.closeId = setObj.closeId;
		}
		if(typeof(setObj.titleId) != 'undefined'){
			this.popSet.titleId = setObj.titleId;
		}
		if(typeof(setObj.backDivColor) != 'undefined'){
			this.popSet.backDivColor = setObj.backDivColor;
		}
		if(typeof(setObj.showAnimate) != 'undefined'){
			this.popSet.showAnimate = setObj.showAnimate;
		}
		var backDivId = this.popSet.backDivId;
		if($('#'+backDivId).length < 1){
			var divStr = '<div id="'+backDivId+'" style="background-color:'+this.popSet.backDivColor+';filter: Alpha(Opacity=12); -moz-opacity:12; opacity:0.12;  position:absolute; left:0px;top:0px; z-index:900"></div>';	
			$(divStr).appendTo('body');
		}
		setDivPox($('#'+backDivId),this);
		$('#'+backDivId).show();
		this.popSet.showAnimate ? this.fadeIn("slow") : this.show();
		var jq = this;
		var ary = this.popSet.closeId.split(',');
		for(var i=0; i<ary.length; ++i){
			$('#'+ary[i]).click(function(){
				jq.popHide();
			});
			$('#'+ary[i]).css({cursor:'pointer'});
		}
		if(this.popSet.titleId != ''){
			divDragGable(this.popSet.titleId , this.attr('id'));
		}
		$(window).resize(function(){
			setDivPox($('#'+backDivId),jq);
		}); 
		$(window).scroll(function(){
			setDivPox($('#'+backDivId),jq,true);
		});
	},

	popHide:function(){
		this.hide();
		$('#'+this.popSet.backDivId).hide();
		$(window).unbind('resize');
		$(window).unbind('scroll');
	}
});
})(jQuery);

